package com.hdu.controller.contentManagement;

import com.hdu.dto.CourseSearchDTO;
import com.hdu.entity.CoursePublish;
import com.hdu.mapper.CoursePublishMapper;
import com.hdu.pojo.PageBean;
import com.hdu.pojo.Result;
import com.hdu.service.ICoursePublishService;
import com.hdu.util.UserUtils;
import com.hdu.vo.CoursePreviewVo;
import com.hdu.vo.CourseSearchVo;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@RestController
@RequiredArgsConstructor
public class CoursePublishController {

    private final ICoursePublishService coursePublishService;
    private final CoursePublishMapper coursePublishMapper;

    /**
     * 课程预览
     *
     * @param courseId 课程id
     * @return ModelAndView
     */
    @GetMapping("/coursePreview/{courseId}")
    public Result<ModelAndView> preview(@PathVariable("courseId") Long courseId) {
        CoursePreviewVo coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course_template");
        modelAndView.addObject("model", coursePreviewInfo);
        return Result.success(modelAndView);
    }

    /**
     * 提交课程审核
     *
     * @param courseId 课程id
     */
    @PostMapping("/courseAudit/commit/{courseId}")
    public Result<?> commitAudit(@PathVariable Long courseId) {
        Long companyId = UserUtils.getLoginUser().getCompanyId();
        coursePublishService.commitAudit(companyId, courseId);
        return Result.success();
    }

    /**
     * 课程发布接口
     *
     * @param courseId 课程id
     */
    @PostMapping("/coursePublish/{courseId}")
    public Result<?> coursePublish(@PathVariable Long courseId) {
        Long companyId = UserUtils.getLoginUser().getCompanyId();
        coursePublishService.publishCourse(companyId, courseId);
        return Result.success();
    }

    @ApiOperation("查询课程发布信息")
    @GetMapping("/r/coursePublish/{courseId}")
    public Result<CoursePublish> getCoursePublish(@PathVariable("courseId") Long courseId) {
        return Result.success(coursePublishService.getCoursePublishCache(courseId));
    }

    @ApiOperation("获取课程发布信息")
    @GetMapping("/course/whole/{courseId}")
    public Result<CoursePreviewVo> getCoursePreviewDto(@PathVariable("courseId") Long courseId) {
        return Result.success(coursePublishService.getCoursePreviewInfo(courseId));
    }


    @ApiOperation("课程分类查询接口")
    @PostMapping("/course/search")
    public Result<PageBean<CourseSearchDTO>> list(@RequestBody CourseSearchVo courseSearchVo) {
        Long companyId = UserUtils.getLoginUser().getCompanyId();
        return Result.success(coursePublishService.queryCourseSearchList(companyId, courseSearchVo));
    }
}
